package P021;

import org.junit.Test;

/**
 * @Author DJ同学
 * @Date 2021/2/9 21:29
 * @Version 1.0
 * @Name 合并两个有序链表
 * @Problem https://leetcode-cn.com/problems/merge-two-sorted-lists/
 * 主要就是对指针的移动
 */
public class Solution {
    @Test
    public void test01(){
        ListNode l1= new ListNode(2);
        ListNode l2= new ListNode(1);
        mergeTwoLists(l1,l2);
    }
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode l3 = new ListNode();
        ListNode root = l3;
        while(l1!=null||l2!=null){
            if(l1!=null&&l2!=null){
                if(l1.val>l2.val){
                    l3.next =  l2;
                    l2=l2.next;
                    l3=l3.next;
                }else{
                    l3.next =  l1;
                    l1=l1.next;
                    l3=l3.next;
                }
            }else if(l1==null){
                l3.next =  l2;
                break;
            }else if(l2==null){
                l3.next =  l1;
                break;
            }
        }
        return root.next;
    }
}
